home *** CD-ROM | disk | FTP | other *** search
- Path: prairienet.org!wemccaug
- From: wemccaug@prairienet.org (Wendy E. McCaughrin)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q]Normalized Random Nos.
- Date: 14 Apr 1996 01:30:18 GMT
- Organization: University of Illinois at Urbana
- Message-ID: <4kpkfa$7or@vixen.cso.uiuc.edu>
- References: <N.040696.101306.34@ix.netcom.com> <4k4gmh$7ol@fountain.mindlink.net>
- Reply-To: wemccaug@prairienet.org (Wendy E. McCaughrin)
- NNTP-Posting-Host: firefly.prairienet.org
-
-
- In a previous article, jhewett@ix.netcom.com (Jerry Hewett) says:
-
- >John (jswalby@mindlink.bc.ca) asks:
- >
- >> Does anybody out there have a C or C++ (msvc++) algorithm to generate
- ##########
- >> normalized random numbers (i.e. between 0 and 1) using the rand() function?
- >
- >Try this out -- it's a modified version of the sample program for rand() that
- >comes with the MSVC online help:
- >
- >----<snip>----
- >
- >/* RAND.C: This program seeds the random-number generator
- > * with the time, then displays 10 random integers.
- > */
- >
- >#include <stdlib.h>
- >#include <stdio.h>
- >#include <time.h>
- >
- >void main( void )
- >{
- > float result;
- > int i;
- >
- > /* Seed the random-number generator with current time so that
- > * the numbers will be different every time we run.
- > */
- >
- > srand( (unsigned)time( NULL ) );
- >
- > /* Display 10 numbers. */
- >
- > for(i = 0;i < 10;i++ )
- > {
- > result = (float) rand ();
- > printf ("%f\n",result / RAND_MAX);
- > }
- >}
-
- No. The builtin random-number generator generates UNIFORMLY-
- distributed pseudo-random numbers, but the author has asked
- for NORMALLY-distributed ones. If you have access to Knuth,
- Vol.II, you will find an algorithm in there for obtaining
- normally-distributed randoms from the uniformaly-dstributed
- ones you get from 'rand'.
-
-